//
//  CLeverydayController.m
//  I-want-go
//
//  Created by dllo on 15/10/23.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "CLeverydayController.h"
#import "BaseModel.h"
#import "CLtool.h"
#import "CLheadCell.h"
#import "CLphoneCollectionViewCell.h"
#import "CLpopularity.h"
#import "CLfootCell.h"
#import "CLfoot.h"
#import "CLscreen.h"
@interface CLeverydayController ()<UITableViewDataSource, UITableViewDelegate, CLheadCellDelegate>
@property(nonatomic, retain)UIImageView *headImage;
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSString *desStr;
@property(nonatomic, retain)NSMutableArray *arr;

@property(nonatomic, retain)NSMutableArray *downArr;
@property(nonatomic, retain)NSMutableArray *smallArr;
@property(nonatomic, retain)NSMutableArray *picArr;
@property(nonatomic, retain)NSMutableArray *nextArr;
@end

@implementation CLeverydayController
- (void)dealloc {
    [_strId release];
    [_picArr release];
    [_downArr release];
    [_nextArr release];
    [_picArr release];
    [_smallArr release];
    [_arr release];
    [_headImage release];
    [_tableView release];
    [_desStr release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor cyanColor];
    
    // tableview创建
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375 * SWIDTH, 667 * SHEIGHT - 63)style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    // 顶头图
    self.headImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, -200 * SHEIGHT, 375 * SWIDTH, 200 * SHEIGHT)];
    self.tableView.tableHeaderView = self.headImage;
    [self.headImage release];
    [self.headImage sd_setImageWithURL:[NSURL URLWithString:self.picStr]];
    
    
    // 解析第一个cell里的数据
    [self createDate];
    
    // 第二个数据
    [self creat];
    
}
/////// 第一个数据
- (void)createDate {
    NSString *stre = @"http://open3.bantangapp.com/community/subject/info";
    NSURL *urli = [NSURL URLWithString:stre];
    NSMutableURLRequest *serequest = [NSMutableURLRequest requestWithURL:urli];
    [serequest setHTTPMethod:@"POST"];
    NSString *strId = [NSString stringWithFormat:@"app_installtime=1445332836.175734&app_versions=4.2.2&channel_name=appStore&client_id=bt_app_ios&client_secret=9c1e6634ce1c5098e056628cd66a17a5&os_versions=8.4.1&screensize=640&subject_id=%@&track_device_info=iPhone4%%2C1&track_deviceid=77AFBC3F-B101-47BE-BE6C-C07187B20EB6&v=7", self.strId];
    NSString *body = strId;
    NSData *bodyD = [body dataUsingEncoding:NSUTF8StringEncoding];
    [serequest setHTTPBody:bodyD];
    [NSURLConnection sendAsynchronousRequest:serequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        self.arr = [NSMutableArray array];
        self.arr = [CLhottopic baseModelByArr:dic[@"data"][@"subject"][@"dynamic"][@"rank_list"]];
        
        self.desStr = dic[@"data"][@"subject"][@"description"];
        
        [self.tableView reloadData];
    }];
}

//////第二个
- (void)creat {
    
        NSURL *url = [NSURL URLWithString:@"http://open3.bantangapp.com/community/subject/listPost"];
    NSMutableURLRequest *serequest = [NSMutableURLRequest requestWithURL:url];
    [serequest setHTTPMethod:@"POST"];
    NSString *sss = [NSString stringWithFormat:@"app_installtime=1445332836.175734&app_versions=4.2.2&channel_name=appStore&client_id=bt_app_ios&client_secret=9c1e6634ce1c5098e056628cd66a17a5&os_versions=8.4.1&page=0&pagesize=10&screensize=640&subject_id=%@&track_device_info=iPhone4%%2C1&track_deviceid=77AFBC3F-B101-47BE-BE6C-C07187B20EB6&type_id=0&v=7", self.strId];
    NSString *body = sss;
    NSData *bodyD = [body dataUsingEncoding:NSUTF8StringEncoding];
    [serequest setHTTPBody:bodyD];
    [NSURLConnection sendAsynchronousRequest:serequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        self.downArr = [NSMutableArray array];
        self.downArr = [CLfoot baseModelByArr:dic[@"data"][@"list"]];
        
        self.smallArr = [NSMutableArray array];
        NSArray *arr = dic[@"data"][@"list"];
        for (NSDictionary *dic1 in arr) {
            CLfoot *fot = [[CLfoot alloc] init];
            [fot setValuesForKeysWithDictionary:dic1[@"author"]];
            [self.smallArr addObject:fot];
            [fot release];
        }
        
        self.picArr = [NSMutableArray array];
        for (NSDictionary *dic in arr) {
            for (NSDictionary *dic1 in dic[@"pics"]) {
                CLfoot *footV = [[CLfoot alloc] init];
                [footV setValuesForKeysWithDictionary:dic1];
                [self.picArr addObject:footV];
                [footV release];
            }
        }
        
        self.nextArr = [NSMutableArray array];
        for (NSDictionary *dic in arr) {
            CLfoot *next = [[CLfoot alloc] init];
            [next setValuesForKeysWithDictionary:dic[@"dynamic"]];
            [self.nextArr addObject:next];
            [next release];
        }
        
        [self.tableView reloadData];
    }];

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else {
        return self.downArr.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        self.tableView.rowHeight = 300 * SHEIGHT;        static NSString *reuse = @"reuse";
        CLheadCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[CLheadCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
            
        }
        cell.contentLabel.text = self.desStr;
        // 传值
        cell.hottpArr = self.arr;
       
        [cell.phoneCollectionView reloadData];
        
        //    协议翻页
        cell.Delegateaa = self;
        return cell;

    }
    else {
        self.tableView.rowHeight = 420 * SHEIGHT;        static NSString *reuse = @"rauu";
        CLfootCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[CLfootCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
            }
        
        CLfoot *foo = self.downArr[indexPath.row];
        cell.timeLabel.text = foo.datestr;
        cell.timeLabel.textColor = [UIColor grayColor];
        cell.describLabel.text = foo.content;
        
        CLfoot *fo = self.smallArr[indexPath.row];
        cell.nameLabel.text = fo.username;
        [cell.leftImage sd_setImageWithURL:[NSURL URLWithString:fo.avatar]];
        CLfoot *footV = self.picArr[indexPath.row];
        [cell.bigImage sd_setImageWithURL:[NSURL URLWithString:footV.url]];        
  
//        cell.likeArr = self.nextArr;
        CLfoot *talk = self.nextArr[indexPath.row];
        cell.thinkLabel.text = @"分享";
        cell.talkLabel.text = [NSString stringWithFormat:@"评论:%@", talk.comments];
        cell.likeLabel.text = [NSString stringWithFormat:@"赞:%@", talk.likes];
        return cell;
    }
}


// 协议方法
- (void)takeValue {
    CLpopularity *popVC = [[CLpopularity alloc] init];
    [self.navigationController pushViewController:popVC animated:YES];
//    NSLog(@"%@", self.arr);
    popVC.hotpicArr = self.arr;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375 * SWIDTH, 50 * SHEIGHT)];
    headerView.backgroundColor = [UIColor whiteColor];
//    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 300, 40)];
//    label.text = @"最  新  精  选";
//    label.textColor = [UIColor redColor];
//    label.font = [UIFont systemFontOfSize:22];
//    
//    [headerView addSubview:label];
//    [label release];
    
    UILabel *bestLabel = [[UILabel alloc] initWithFrame:CGRectMake(130 * SWIDTH, 0, 100 * SWIDTH, 40 * SHEIGHT)];
    bestLabel.text = @"  精          选";
        [headerView addSubview:bestLabel];
    [bestLabel release];
    
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(50 * SWIDTH, 20 * SHEIGHT, 70 * SWIDTH, 1 * SHEIGHT)];
    leftView.backgroundColor = [UIColor blackColor];
    [headerView addSubview:leftView];
    [leftView release];
    
    UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(240 * SWIDTH, 20 * SHEIGHT, 70 * SWIDTH, 1 * SHEIGHT)];
    rightView.backgroundColor = [UIColor blackColor];
    [headerView addSubview:rightView];
    [rightView release];

    
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        return 40 * SHEIGHT;
    } else {
        return 0;
    }
}



- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO;
}



















- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
